package netTest;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
    public static void main(String[] args) throws IOException {
        //获取本机ip地址对应的InetAddress实例
        InetAddress ip = InetAddress.getLocalHost();
        System.out.println(ip);
        //获取原始IP地址
        byte[] bytes = ip.getAddress();
        for(byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();
        //获取主机名
        String host = ip.getHostName();
        //通过主机名和原始ip地址获得InetAddress实例
        InetAddress ip1 = InetAddress.getByAddress(host,bytes);
        System.out.println(ip1);
        System.out.println(host);
        //根据主机名获得对应的inetAddress实例
        InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
        System.out.println(inetAddress);
        //获取此IP地址的完全限定域名。
        System.out.println(inetAddress.getCanonicalHostName());
        //测试该地址是否可达。
        System.out.println(inetAddress.isReachable(1000));

    }
}
